﻿#region
using System;
using System.Collections;
using System.Data;
using System.Web.UI.WebControls;
using ZhongLangDB;
#endregion

namespace ZHONGLANGWEBUI.ZHONGLANG.SYS
{
    public partial class USER : TT_BasePageList
    {
        protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { pageInit(); dgListDo("查询初次", ""); } }

        #region pageInit
        protected override void pageInit()
        {
            DataTable dtCom = proGETCOM();
            DDLBind(dtCom, "COMCODENAME", "COMID", true, ref ddlQCOMID); DDLBindValue(CURREMP.COMID, ref ddlQCOMID); ddlQCOMID_OnSelectedIndexChanged(null, null);
            DDLBind(dtCom, "COMCODENAME", "COMID", false, ref ddlECOMID); DDLBindValue(CURREMP.COMID, ref ddlECOMID); ddlECOMID_OnSelectedIndexChanged(null, null); 

            DataTable dtRole = DBPark.proGetDT(null, "SELECT ROLEID,ROLECODE+' ' +ROLENAME ROLECODENAME FROM TT_SYS_ROLE WHERE ROLESTATE=1");
            DDLBind(dtRole, "ROLECODENAME", "ROLEID", true, ref ddlQROLEID);
            DDLBind(dtRole, "ROLECODENAME", "ROLEID", false, ref ddlEROLEID);

            DataTable dtUserType = DBPark.proGetDT(null, "SELECT DISTINCT USERTYPE FROM TT_SYS_USER WHERE USERSTATE=1");
            DDLBind(dtUserType, "USERTYPE", "USERTYPE", true, ref ddlQUSERTYPE); 

            DDLBindStr("0$注册,1$正常,9$停用", true, ref ddlQUSERSTATE);
            DDLBindStr("0$注册,1$正常,9$停用", false, ref ddlEUSERSTATE);
        }
        protected void ddlQCOMID_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlQCOMID.SelectedValue == "") { return; }
            DataTable dtDEPTPRO = proGETDEPTPRO(ddlQCOMID.SelectedValue);
            DDLBind(dtDEPTPRO, "DEPTCODENAME", "DEPTID", true, ref ddlQDEPTPRO); DDLBindValue(CURREMP.DEPTPRO, ref ddlQDEPTPRO);
            ddlQDEPTPRO_OnSelectedIndexChanged(null,null);
        }
        protected void ddlQDEPTPRO_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlQDEPTPRO.SelectedValue == "") { return; }
            DataTable dtDEPT = proGETDEPT(ddlQCOMID.SelectedValue, ddlQDEPTPRO.SelectedValue);
            DDLBind(dtDEPT, "DEPTCODENAME", "DEPTID", true, ref ddlQDEPTID); DDLBindValue(CURREMP.DEPTID, ref ddlQDEPTID);
        }

        protected void ddlECOMID_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlECOMID.SelectedValue == "") { return; }
            DataTable dtDEPTPRO = proGETDEPTPRO(ddlECOMID.SelectedValue);
            DDLBind(dtDEPTPRO, "DEPTCODENAME", "DEPTID", false, ref ddlEDEPTPRO); DDLBindValue(CURREMP.DEPTPRO, ref ddlEDEPTPRO);
            ddlEDEPTPRO_OnSelectedIndexChanged(null, null);
        } 
        protected void ddlEDEPTPRO_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlEDEPTPRO.SelectedValue == "") { return; }
            DataTable dtDEPT = proGETDEPT(ddlECOMID.SelectedValue, ddlEDEPTPRO.SelectedValue);
            DDLBind(dtDEPT, "DEPTCODENAME", "DEPTID", false, ref ddlEDEPTID); DDLBindValue(CURREMP.DEPTID, ref ddlEDEPTID);
        }
        #endregion

        #region pageBind 
        protected DataTable proSDListQuery(int iType)
        {
            SortedList SDList = new SortedList();
            SDList["COMID"] = ddlQCOMID.SelectedValue;
            SDList["DEPTPRO"] = ddlQDEPTPRO.SelectedValue;
            SDList["DEPTID"] = ddlQDEPTID.SelectedValue;
            SDList["DEPTNAME"] = txtQDEPTNAME.Text;

            SDList["ROLEID"] = ddlQROLEID.SelectedValue;
            SDList["USERINFO"] = txtQUSERINFO.Text; 
            SDList["USERSTATE"] = ddlQUSERSTATE.SelectedValue;
            DataTable dt = pgShow.proQueryData(SDList, "PROTT_SYS_USERQUERY", iType);
            return dt;
        }

        protected override void pageBind()
        {
            DataTable dt = proSDListQuery(1);
            DataView dv = dt.DefaultView;
            if (ViewState["SortExpression"] != null) { dv.Sort = GetSortDirectiont(ViewState["SortExpression"].ToString(), ViewState["SortDirection"].ToString()); }
            dgList.DataSource = dv; dgList.DataBind();
        }
        #endregion

        #region pageOpt 
        protected override void dgListDo(string strCommandName, string strCommandArgument)
        {
            #region dgBase 
            if (strCommandName == "查询初次") { pgShow.CurrPageIndex = 1; pgShow.CurrPageSize = REQPAGECOUNT; pageBind(); }
            if (strCommandName == "查询") { pageBind(); tabQuery.Visible = true; tabShow.Visible = true; tabEdit.Visible = false; }
            if (strCommandName == "导出") { proExpData(proSDListQuery(2), dgList); }
            if (strCommandName == "关闭") { tabQuery.Visible = true; tabShow.Visible = true; tabEdit.Visible = false; }
            if (strCommandName == "添加") { txtEUSERNAME.Text = ""; txtEUSERLOGIN.Text = ""; chkESETPASS.Checked = false; lblUpdate.Text = ""; btnSave.CommandArgument = ""; tabQuery.Visible = false; tabShow.Visible = false; tabEdit.Visible = true; }
            #endregion
             
            #region 修改 
            if (strCommandName == "修改")
            {
                SortedList SDList = new SortedList();
                SDList["KEYID"] = strCommandArgument;
                DataTable dt = DBPark.proGetDT(SDList, "PROTT_SYS_USERQUERY");
                if (dt != null && dt.Rows.Count > 0)
                {
                    DataRow dr = dt.Rows[0];
                    txtEUSERLOGIN.Text = dr["USERLOGIN"].ToString();
                    DDLBindValue(dr["USERSTATE"].ToString(), ref ddlEUSERSTATE);

                    txtEUSERNAME.Text  = dr["USERNAME"].ToString();
                    txtEUSERMIBIO.Text = dr["USERMIBIO"].ToString();
                    txtEUSEREMAIL.Text = dr["USEREMAIL"].ToString();

                    DDLBindValue(dr["COMID"].ToString(), ref ddlECOMID);  ddlECOMID_OnSelectedIndexChanged(null, null); 
                    DDLBindValue(dr["DEPTPRO"].ToString(), ref ddlEDEPTPRO); ddlEDEPTPRO_OnSelectedIndexChanged(null, null);
                    DDLBindValue(dr["DEPTID"].ToString(), ref ddlEDEPTID);

                    DDLBindValue(dr["ROLEID"].ToString(), ref ddlEROLEID);
                    txtEUSERTYPE.Text = dr["USERTYPE"].ToString();

                    chkESETPASS.Checked = false;
  
                    aImgEUSERHEAD.ImageUrl = dr["USERHEAD"].ToString();
                    aImgEUSERMARK.ImageUrl = dr["USERMARK"].ToString();
                    txtEditor.Text = DBPark.txtFile.txtReadFromFile(CURRPATH(), dr["USERXML"].ToString(), "COMUSERXML");
                    hidEditorPath.Value = dr["USERXML"].ToString(); 

                    lblUpdate.ForeColor = System.Drawing.Color.Blue;
                    lblUpdate.Text = dr["UPDATEMANNAME"].ToString() + "【" + strDateFmtTime(dr["UPDATETIME"].ToString()) + "编辑】";
                    btnSave.CommandArgument = strCommandArgument; tabQuery.Visible = false; tabEdit.Visible = true; tabShow.Visible = false;

                }
                else { jsMessage("参数有误，未找到对应记录！"); }
            }
            #endregion

            #region 保存
            if (strCommandName == "保存")
            {
                SortedList SDList = new SortedList();
                SDList["USERLOGIN"] = txtEUSERLOGIN.Text;
                SDList["USERSTATE"] = ddlEUSERSTATE.SelectedValue;
                if (chkESETPASS.Checked || btnSave.CommandArgument == "") { SDList["USERPASS"] = DBPark.strChange.MD5PHP("111111"); }

                SDList["USERNAME"] = txtEUSERNAME.Text;
                SDList["USERMIBIO"] = txtEUSERMIBIO.Text;
                SDList["USEREMAIL"] = txtEUSEREMAIL.Text;

                SDList["COMID"] = ddlECOMID.SelectedValue;
                SDList["DEPTPRO"] = ddlEDEPTPRO.SelectedValue;
                SDList["DEPTID"] = ddlEDEPTID.SelectedValue;

                SDList["ROLEID"] = ddlEROLEID.SelectedValue;
                SDList["USERTYPE"] = txtEUSERTYPE.Text;

                if (fupEUSERHEAD.PostedFile.FileName != "") {
                    SDList["USERHEAD"] = "/ZHONGLANG/FILE/COMUSERHEAD/" + ddlECOMID.SelectedItem.Text.Split(' ')[0] + "_" + ddlEDEPTID.SelectedItem.Text.Split(' ')[0] + "_" + SDList["USERLOGIN"].ToString() + DateTime.Now.ToString("yyMMddHHmmss") + ".png";
                    fupEUSERHEAD.PostedFile.SaveAs(MapPath(SDList["USERHEAD"].ToString()));
                }
                if (fupEUSERMARK.PostedFile.FileName != "") {
                    SDList["USERMARK"] = "/ZHONGLANG/FILE/COMUSERMARK/" + ddlECOMID.SelectedItem.Text.Split(' ')[0] + "_" + ddlEDEPTID.SelectedItem.Text.Split(' ')[0] + "_" + SDList["USERLOGIN"].ToString() + DateTime.Now.ToString("yyMMddHHmmss") + ".png";
                    fupEUSERMARK.PostedFile.SaveAs(MapPath(SDList["USERMARK"].ToString()));
                }
                SDList["USERXML"] = (btnSave.CommandArgument == "" ? "USERXML_" + ddlECOMID.SelectedItem.Text.Split(' ')[0] + "_" + ddlEDEPTID.SelectedItem.Text.Split(' ')[0] + "_" + SDList["USERLOGIN"].ToString() + DateTime.Now.ToString("yyMMddHHmmss") + ".htm" : hidEditorPath.Value);
               
                SDList["KEYID"] = btnSave.CommandArgument; SDList["UPDATEMANID"] = CURREMP.USERID; SDList["UPDATEMANNAME"] = CURREMP.USERNAME;
                string strBack = DBPark.proSetDB(SDList, "PROTT_SYS_USERSAVE");
                if (strBack != "" && strBack.Substring(0,2) != "OK") { lblUpdate.Text = strBack; lblUpdate.ForeColor = System.Drawing.Color.Red; jsMessage(strBack); }
                else { 
                    DBPark.txtFile.txtSaveToFile(txtEditor.Text, CURRPATH(), SDList["USERXML"].ToString(), "COMUSERXML", true);
                    dgListDo("查询", ""); }
            }
            #endregion 
        }
        #endregion

  

        #region pageOpt 
        //protected override void dgListDo(string strCommandName, string strCommandArgument)
        //{
        //    #region 新建 修改
        //    if (strCommandName == "新建")
        //    {
        //        fmtAdd();
        //        lblUpdate.Text = ""; btnSave.CommandArgument = ""; tabQuery.Visible = false; tabEdit.Visible = true; tabShow.Visible = false;
        //    }
        //    if (strCommandName == "修改")
        //    {
        //        SortedList SDList = new SortedList();
        //        SDList["KEYID"] = strCommandArgument;
        //        DataTable dt = DBPark.proGetDT(SDList, strPROCHEAD + "QUERY");
        //        if (dt != null && dt.Rows.Count > 0)
        //        {
        //            DataRow dr = dt.Rows[0];

        //            fmtOneShow(dr);

        //            lblUpdate.ForeColor = System.Drawing.Color.Blue;
        //            lblUpdate.Text = dr["UPDATEMANNAME"].ToString() + "【" + strDateFmtTime(dr["UPDATETIME"].ToString()) + "编辑】";
        //            btnSave.CommandArgument = strCommandArgument; tabQuery.Visible = false; tabEdit.Visible = true; tabShow.Visible = false;

        //        }
        //        else { jsMessage("参数有误，未找到对应记录！"); }
        //    }
        //    #endregion

        //    #region 保存
        //    if (strCommandName == "保存")
        //    {
        //        SortedList SDList = currGetSaveSDList();
        //        SDList["KEYID"] = btnSave.CommandArgument; SDList["UPDATEMANID"] = CURREMP.USERID; SDList["UPDATEMANNAME"] = CURREMP.USERNAME;
        //        string strBack = DBPark.proSetDB(SDList, strPROCHEAD + "SAVE");
        //        if (strBack != "") { lblUpdate.Text = strBack; lblUpdate.ForeColor = System.Drawing.Color.Red; jsMessage(strBack); }
        //        else {
        //            if (fupEPIC.PostedFile.FileName != "")
        //            {
        //                string strUserIDSql = "SELECT TOP 1 USERID FROM TT_SYS_USER WHERE USERLOGIN = '" + DBPark.proPrameCheck(txtEUSERLOGIN.Text) + "'";
        //                string strPath = "../USER/HEAD/" + DBPark.proGetDTOne(null, strUserIDSql) + ".png";
        //                fupEPIC.PostedFile.SaveAs(MapPath(strPath));
        //            }
        //            dgListBaseDo("关闭", ""); }
        //    }
        //    #endregion 
        //}
        #endregion 
          
    }
}